<?php
ob_start();
/*
	控件每次向此文件POST数据
	逻辑：
		1.更新数据库进度
		2.将文件块数据保存到服务器中。
	更新记录：
		2014-04-09 增加文件块验证功能。
		2017-05-29 完善逻辑
		2017-08-17 取消对redis的引用
*/
require '../../vendor/autoload.php';

use model\FileInf;
use store\FileBlockWriter;
use utils\PathTool;
use utils\UtilsTool;
use utils\ConfigReader;
use utils\CryptoTool;
use utils\FileTool;
use utils\HttpHeader;
use biz\up7_biz_event;
use biz\WebSafe;
use biz\PathBuilder;

$head            = new HttpHeader();
$uid             = $head->param("uid");
$id			     = $head->param("id");
$pid		     = $head->param("pid");
$pidRoot	     = $head->param("pidRoot");
$perSvr          = $head->param("perSvr");
$lenSvr          = $head->param("lenSvr");
$lenLoc          = $head->param("lenLoc");
$sizeLoc         = $head->param("sizeLoc");
$nameLoc         = $head->param("nameLoc");
$blockSizeCry    = $head->paramInt("blockSizeCry");//加密块大小
$blockSizeCpr    = $head->paramInt("blockSizeCpr");
$blockEncrypt    = $head->paramBool("blockEncrypt");//加密标识
$blockOffset     = $head->param("blockOffset");
$blockIndex      = $head->param("blockIndex");
$blockCount      = $head->param("blockCount");
$blockSize       = $head->param("blockSize");//逻辑块大小信息，提供给下载使用
$blockSizeLogic  = $head->param("blockSizeLogic");//逻辑块大小信息，提供给下载使用
$blockMd5		 = $head->param("blockMd5");
$blockCompress   = $head->paramBool("blockCompress");
$blockCompressType = $head->param("blockCompressType");
$encryptAlgorithm  = $head->param("blockEncryptAgo");
$complete        = strcmp("true",$head->param("complete"))==0;
$object_id       = $head->param("object_id");
$token           = $head->param("token");
$pathLoc         = $_POST["pathLoc"];
$pathSvr         = $_POST["pathSvr"];
$pathRel         = $_POST["pathRel"];
$pathLoc         = PathTool::url_decode($pathLoc);
$pathSvr         = PathTool::url_decode($pathSvr);
$pathRel         = PathTool::url_decode($pathRel);
$fpath           = $_FILES['file']['tmp_name'];//如果接收不到数据请参考教程：http://bbs.ncmem.com/thread-196-1-1.html

if(empty($pathLoc)) $pathLoc = $_FILES['file']['name'];

//相关参数不能为空
if (   (strlen($lenLoc)>0) 
	&& (strlen($uid)>0) 
	&& (strlen($id)>0) 
	&& (strlen($blockOffset)>0))
{		
    $msg = "";
    $md5Svr = "";
    $stm = FileTool::readAll($fpath);
    
    $fileSvr             = new FileInf();
    $fileSvr->id         = $id;
    $fileSvr->pid        = $pid;
    $fileSvr->pidRoot    = $pidRoot;
    $fileSvr->object_id  = $object_id;
    $fileSvr->encryptAlgorithm = $encryptAlgorithm;
    $fileSvr->lenSvr     = $lenSvr;
    $fileSvr->lenLoc     = $lenLoc;
    $fileSvr->sizeLoc    = $sizeLoc;
    $fileSvr->pathLoc    = $pathLoc;
    $fileSvr->pathSvr    = $pathSvr;
    $fileSvr->pathRel    = $pathRel;
    $fileSvr->blockIndex = intval($blockIndex);
    $fileSvr->blockOffset= intval($blockOffset);
    $fileSvr->blockCount = intval($blockCount);
    $fileSvr->blockSize  = $blockSize;
    $fileSvr->blockCompress = $blockCompress;
    $fileSvr->blockCompressType = $blockCompressType;
    $fileSvr->blockEncrypt  = $blockEncrypt;
    $fileSvr->blockSizeCry  = $blockSizeCry;
    $fileSvr->blockSizeCpr  = $blockSizeCpr;
    $fileSvr->nameLoc    = PathTool::getName($fileSvr->pathLoc);
    $fileSvr->nameSvr    = $fileSvr->nameLoc;
    $fileSvr->encrypt    = ConfigReader::storageEncrypt();//加密存储
    
    //md5验证
    if(!UtilsTool::check_md5($stm,$blockMd5))
    {
        $md5Svr = md5($stm);
        $obj = array(
            'msg'=>'blockMd5ValidFail',
            'errCode'=>'blockMd5ValidFail',
            'md5Svr'=>$md5Svr,
            'md5Loc'=>$blockMd5,
            'offset'=>-1
        );
        exit(json_encode($obj));
    }
    
    //解压缩
    $stm = UtilsTool::unCompress($stm,$fileSvr);
    
    //解密
    $stm = UtilsTool::decrypt($stm,$fileSvr);
    
    //token验证
    $ws = new WebSafe();
    if(!$ws->validToken($token,$fileSvr,"block"))
    {
        $obj = array(
            'msg'=>'tokenValidFail',
            'errCode'=>'tokenValidFail',
            'md5'=>"",
            'offset'=>-1
        );
        exit(json_encode($obj));
    }
	
    $pb = new PathBuilder();
    $fileSvr->pathSvr = $pb->relToAbs($fileSvr->pathSvr);
    
	//保存块
	$fw = new FileBlockWriter();
	if($fileSvr->blockIndex == 1) $fw->make($fileSvr);
	$fw->write($fileSvr, $stm);
	
	//触发事件
	up7_biz_event::file_post_block($id, $blockIndex);
	
	$obj = array('msg'=>'ok',
	    'offset'=>$blockOffset,
	    'fields'=>array('object_id'=>$fileSvr->object_id)
	);
	$msg = json_encode($obj);
	echo $msg;
}
else
{
	echo "param is null";
}
header('Content-Length: ' . ob_get_length());
?>